
var el1 = null;
$(function() {
	
	layui.config({
	    base: rootPath + "/plugin/layui-v2.4.5/eleTree/"
	}).use(['jquery','table','eleTree','code','form','slider'], function(){
	    var $ = layui.jquery;
	    var eleTree = layui.eleTree;
	    var table = layui.table;
	    var code = layui.code;
	    var form = layui.form;
	    var slider = layui.slider;

	    var roleId = $("#roleId").val();
	    var url_role = rootPath + "/sys/menu/findRoleMenu";
	    var menu_role = CommnUtil.ajax(url_role, {
	        "roleId" : roleId
	    }, "json");
	    var menu_role_arr = [];
	    if(menu_role == null || menu_role.length > 0){
	    	console.log(menu_role);
	    	for(var i=0; i < menu_role.length; i ++){
	    		if(menu_role[i].pid != null){
	    			menu_role_arr.push(menu_role[i].id);
	    		}
	    	}
	    }
	    
	    var url_all = rootPath + "/sys/menu/findAllMenu";
	    var menu_all = CommnUtil.ajax(url_all, {}, "json");
	    var data = [];
	    if(menu_all == null || menu_all.length > 0){
		    for(var i = 0; i < menu_all.length; i ++){
		    	var parent = menu_all[i];
		    	if(parent.pid == null || parent.pid == ""){
		    		var val = {
		    			"id" : parent.id,
		    			"label" : parent.menuName
		    		};
		    		//处理子类
		    		var children = [];
		    		for(var j = 0; j < menu_all.length; j ++){
		    			var child = menu_all[j];
		    			//判断父子关系
		    			if(child.pid == parent.id){
		    				var val_c = {
		    					"id" : child.id,
		    		    		"label" : child.menuName
		    				}
		    				children.push(val_c);
		    			}
		    		}
		    		val["children"] = children;
		    		data.push(val);
		    	}
		    }
	    	
	    }
	    var obj={
	        elem: '.ele1',
	        data: data,
	        // url: "../eleTree/tree.json",
	        renderAfterExpand: true,
	        expandOnClickNode: true,
	        showCheckbox: true,
	        defaultExpandAll: true,
	        defaultCheckedKeys: menu_role_arr
	    }
	    el1 = eleTree.render(obj);
	    
	});
	
	// 绑定取消按钮事件
	$(".cancel").click(function() {
	    parent.layer.close(parent.pageii);
	});
	
//	var el5;
	
});

//提交
function sub(){
	var list = el1.getChecked(false, true);
	var ids = "";
	if(list != null && list.length > 0){
		list.forEach(function( item, index ) {
			if(ids.length == 0){
				ids = item.id;
			}else{
				ids += "," + item.id;
			}
		});
	}
	
	var roleId = $("#roleId").val();
	var url = rootPath + "/sys/menu/addMenuRight";
	var res = CommnUtil.ajax(url, {
        "roleId" : roleId,
        "ids" : ids
    });
	if(res == 'success'){
		layer.msg("菜单添加成功！");
		parent.grid.loadData();
        parent.layer.close(parent.pageii);
	}else{
		layer.msg('添加失败！');
	}
}

